package com.hetwen.search.model;

import android.content.Context;
import cn.android.common.util.StringUtils;

import com.hetwen.search.cache.ACache;
import com.hetwen.search.http.API;
import com.hetwen.search.http.IResponseHandler;
import com.hetwen.search.utils.JsonUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.PreferencesCookieStore;

public class DataManager {

	protected static final HttpMethod GET = HttpMethod.GET;

	protected static final HttpMethod POST = HttpMethod.POST;

	private static DataManager manager;

	protected ACache mCache;

	private HttpUtils httpUtils;

	public static synchronized DataManager getInstance(Context context) {
		if (manager == null) {
			manager = new DataManager(context);
		}
		return manager;
	}

	private DataManager(Context context) {
		initHttp(context);
		initCache(context);
	}

	private void initHttp(Context context) {
		httpUtils = new HttpUtils(10000);
		httpUtils.configSoTimeout(10000);
		httpUtils.configTimeout(10000);
		httpUtils.configRequestThreadPoolSize(10);
		httpUtils.configCurrentHttpCacheExpiry(3000);
		httpUtils.configDefaultHttpCacheExpiry(0);
		configCookies(context);
	}

	private void configCookies(Context context) {
		PreferencesCookieStore cookieStore = new PreferencesCookieStore(context);
		httpUtils.configCookieStore(cookieStore);
	}

	private void initCache(Context context) {
		mCache = ACache.get(context);
	}

	/**
	 * 
	 * @param keyword  关键词
	 * @param rn      
	 * @param pn
	 * @param handler
	 */
	public void searchImage(String keyword, int rn, int pn,
			final IResponseHandler handler) {
		// 添加参数
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("tn", "baiduimagejson");
		params.addQueryStringParameter("width", "");
		params.addQueryStringParameter("height", "");
		params.addQueryStringParameter("word", keyword);
		params.addQueryStringParameter("ie", "utf-8");
		params.addQueryStringParameter("rn", rn + "");
		params.addQueryStringParameter("pn", pn + "");

		httpUtils.send(GET, API.BASE_URL, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException e, String error) {
						if(handler!=null){
							handler.onFailure(e.getExceptionCode()+"", error);
						}	
					}

					@Override
					public void onSuccess(ResponseInfo<String> resposne) {
						String result = resposne.result;
						if(!StringUtils.isBlank(result)){
							try {
								BDImage bdimage = (BDImage) JsonUtils.parse(
										BDImage.class, result);
								if(handler!=null){
									handler.onSuccess(result, bdimage);
								}
							} catch (Exception e) {
								e.printStackTrace();
								if(handler!=null){
									handler.onFailure("-1", e.getMessage());
								}
							}
							
						}else{
							handler.onFailure("-1", "未获取到数据！");
						}
					}
				});
	}

}
